package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.dao.CollectionFormDao;
import com.py.credit.entity.AreaRelationEntity;
import com.py.credit.entity.CollectionDataEntity;
import com.py.credit.entity.CollectionForm;
import com.py.credit.entity.Dept;
import com.py.credit.service.CollectionFormService;
import com.py.credit.service.RegionRelationService;
import com.py.framework.core.entity.SysAreaEntity;
import com.py.framework.core.entity.SysOfficeEntity;

@Service
public class CollectionFormServiceImpl implements CollectionFormService {

	@Autowired
	CollectionFormDao collectionFormDao;

	@Autowired
	private RegionRelationService regionRelationService;

	@Override
	public void deleteInfo() {
		collectionFormDao.deleteInfo();

	}

	@Override
	@Deprecated
	public List<Dept> selectAllDept() {
		return collectionFormDao.selectAllDept();
	}

	@Override
	public void addDeptData(int dept_id, int target, int num, String dateTime) {
		collectionFormDao.addDeptData(dept_id, target, num, dateTime);
	}

	@Override
	public Map<String, Object> getDeptNum(RestServiceResult<List<SysAreaEntity>> allAreaInfo,
			Map<Long, String> deptInfo, List<SysOfficeEntity> allDepts) {
		List<CollectionForm> list = new ArrayList<CollectionForm>();
		Map<String, Object>map = new HashMap<String, Object>(2);
		Map<String, Object>map1 = new HashMap<String, Object>(2);
		int deptNum = 0;
		int total = 0;
		List<CollectionForm> collectionForms = null;
		collectionForms = collectionFormDao.selectRegionNum();
		Map<Integer, CollectionForm> froms = new HashMap<Integer, CollectionForm>(collectionForms.size());
		if (!CollectionUtils.isEmpty(collectionForms)) {
			for (CollectionForm collectionForm : collectionForms) {
				froms.put(collectionForm.getDeptId(), collectionForm);
			}
		}
		// 组装区域资源信息
		Map<Long, AreaRelationEntity> areas = regionRelationService.dealRegionResource(allAreaInfo);
		// 组装部门资源信息 返回部门对应资源list关系
		Map<Long, List<String>> deptResources = regionRelationService.dealDeptResourceList(areas, allDepts);
		for (Entry<Long, List<String>> entry : deptResources.entrySet()) {
			// 部门数统计++
			deptNum++;
			Long deptId = entry.getKey();
			Integer id = Integer.parseInt(String.valueOf(deptId));
			CollectionForm form = froms.get(id);
			if (null != form) {
				form.setDeptName(deptInfo.get(deptId));
				List<String> l = deptResources.get(deptId);
				form.setXynum(CollectionUtils.isEmpty(l) ? 0 : l.size());
				if(form.getDeptId()!=51) {
				total = total + form.getSjnum();
				}
			} else {
				form = new CollectionForm();
				form.setDeptId(id);
				form.setDeptName(deptInfo.get(deptId));
				List<String> l = deptResources.get(deptId);
				form.setXynum(CollectionUtils.isEmpty(l) ? 0 : l.size());
				form.setSjnum(0);
			}
			if(form.getDeptId()!=51) {
				list.add(form);
			}
		
		}
		
		map1.put("deptNum", deptNum);
		map1.put("num", total);
		map.put("totalNum", map1);
		map.put("deptNum", list);
		return map;
	}

	@Override
	public List<CollectionForm> getDataForDeptId(String deptId) {
		List<CollectionForm> list =collectionFormDao.getDataForDeptId(deptId);
		return list;
	}

	/**
	 * 
	 * @Function 总体数据归集
	 * @FileName CollectionFormServiceImpl.java
	 * @MethodName collectData
	 * @Date 2018年9月19日 下午2:53:31
	 * @Author Min DongXv
	 *
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void collectData() {
		// 查询总体归集数据
		List<CollectionDataEntity> collectionData = collectionFormDao.selectDeptNum();
		if (!CollectionUtils.isEmpty(collectionData)) {
			// 删除归集数据
			collectionFormDao.deleteInfo();
			// 新增归集数据
			collectionFormDao.batchAddCollectionData(collectionData);
			// 获取归集数量
			Integer i = collectionFormDao.getCollectionDataCount();
			if (null == i || i <= 0) {
				ExceptionLogger.error("CollectionFormServiceImpl#collectData", "归集数据异常！未查询到归集数据！");
				throw new ServiceException("归集数据异常！未查询到归集数据！");
			}
		}
	}

	/**
	 *
	 * @Function 批量新增归集数量
	 * @FileName CollectionFormServiceImpl.java
	 * @MethodName batchAddCollectionData
	 * @param list
	 * @Date 2018年9月19日 下午2:29:08
	 * @Author Min DongXv
	 *
	 */
	@Override
	public void batchAddCollectionData(List<CollectionDataEntity> list) {
		collectionFormDao.batchAddCollectionData(list);
	}
}
